/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package uk.org.aspellclark.agenedb.view.individual;

import java.util.List;

import uk.org.aspellclark.agenedb.AGeneDb;
import uk.org.aspellclark.agenedb.Preferences;
import uk.org.aspellclark.agenedb.R;
import uk.org.aspellclark.common.ImageFunctions;
import uk.org.aspellclark.gedcom.model.records.IndividualRecord;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class IndividualListRowAdapter extends ArrayAdapter<IndividualRecord> implements View.OnClickListener {

	static class IndividualListRowAdapterViewHolder {
		public TextView txtIndividualId;
		public TextView txtNameFirst;
		public TextView txtNameLast;
		public TextView txtDates;
		public ImageView imgPicture;
	}

	private List<IndividualRecord> items;

	private LayoutInflater mInflater = (LayoutInflater) this.getContext().getSystemService(
			Context.LAYOUT_INFLATER_SERVICE);

	public IndividualListRowAdapter(Context context, int textViewResourceId, List<IndividualRecord> items) {
		super(context, textViewResourceId, items);
		this.items = items;
	}

	@Override
	public int getCount() {
		return this.items.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		IndividualListRowAdapterViewHolder holder;

		if (convertView == null) {
			convertView = this.mInflater.inflate(R.layout.individuals_list_view_row, null);
			convertView.setOnClickListener(this);

			holder = new IndividualListRowAdapterViewHolder();
			holder.imgPicture = (ImageView) convertView.findViewById(R.id.individualsListLayoutImage);
			holder.txtIndividualId = (TextView) convertView.findViewById(R.id.txtIndividualId);
			holder.txtNameFirst = (TextView) convertView.findViewById(R.id.individualsListLayoutTextNamesFirstname);
			holder.txtNameLast = (TextView) convertView.findViewById(R.id.individualsListLayoutTextNamesLastname);
			holder.txtDates = (TextView) convertView.findViewById(R.id.individualsListLayoutDatesString);

			convertView.setTag(holder);
		} else {
			holder = (IndividualListRowAdapterViewHolder) convertView.getTag();
		}
		IndividualRecord o = this.items.get(position);
		if (o.noIndividualsInDatabase) {
			holder.txtNameFirst.setText(" ");
			holder.txtNameLast.setText("Database is Empty");
			holder.txtDates.setText("Please import a Gedcom File");
			holder.txtIndividualId.setText("Database is Empty");

		} else {
			Bitmap bmp = null;
			if (o.multimediaLink != null && o.multimediaLink.size() > 0) {
				//set the image from the first multimedia item
				//MultimediaLink mm = o.multimediaLink.get(0);
				//				if (mm.fileReference != null && mm.fileReference.length() > 0) {
				//					File tmpf = new File(mm.fileReference);
				//					String newFilename = AGeneDb.getGedomImagePathDir() + tmpf.getName();
				//					bmp = ImageFunctions.getImage(newFilename);
				//					if (bmp == null) {
				//						bmp = ImageFunctions.getImage(AGeneDb.getGedomPathDir() + tmpf.getName());
				//					}
				//				}
			} else {
				bmp = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.unknown_prsn);
			}
			if (bmp == null) {
				bmp = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.unknown_prsn);
			}
			holder.imgPicture.setImageDrawable(new BitmapDrawable(ImageFunctions.scaleImage(bmp, 48)));
			holder.txtIndividualId.setText(o.xref);
			holder.txtNameFirst.setText(o.getNameFirst());
			holder.txtNameLast.setText(" /" + o.getNameLast() + "/ ");
			holder.txtDates.setText("from: " + o.getDOB() + " to " + o.getDOD());

			if (o.sex != null && o.sex.equalsIgnoreCase("M")) {
				convertView.setBackgroundResource(R.drawable.ind_lst_ind_male);
			} else if (o.sex != null && o.sex.equalsIgnoreCase("F")) {
				convertView.setBackgroundResource(R.drawable.ind_lst_ind_female);
			} else {
				convertView.setBackgroundResource(R.drawable.ind_lst_ind);
			}
		}
		return convertView;
	}

	@Override
	public void onClick(View v) {
		if (v instanceof LinearLayout) {
			TextView txtIndividualId = (TextView) v.findViewById(R.id.txtIndividualId);
			Preferences.currentIndividualId = txtIndividualId.getText().toString();
			if (Preferences.currentIndividualId.equals("Database is Empty")) {
				Preferences.currentIndividualId = "";
				return;
			}

			Context ctx = this.getContext();
			AGeneDb agdb = (AGeneDb) ctx;
			agdb.updateCurrentPerson(Preferences.currentIndividualId);
		}
	}

}
